package com.cdsxt.genCode.dao;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

public interface TableDao {
	
	/**
	 * 查询指定的表信息
	 * TODO 增加指定表信息的获取，还有忽略某些表的信息获取
	 */
	@Select("select * from information_schema.TABLES where TABLE_SCHEMA=(select database())"
			+ "and TABLE_NAME in ( ${tableNames})")
	public List<Map<String, ?>> selectListTableForNames(@Param("tableNames") String tableNames);
	public static class Util{
		public static String getListStr(List<String> list) {
			StringBuilder tableNames=new StringBuilder();
			for (int idx=0;idx<=list.size()-1;idx++) {
				String tabName=list.get(idx);
				//tableNames+=""tabName;
				tableNames.append("'");
				tableNames.append(tabName);
				tableNames.append("'");
				if (idx!=list.size()-1) {
					tableNames.append(",");	
				}
			}
			return tableNames.toString();
		}
	}
	
	/**
	 * 查询所有的表
	 */
	@Select("select * from information_schema.TABLES where TABLE_SCHEMA=(select database())")
	public List<Map<String, ?>> selectListTable();


	@Select("select * from information_schema.COLUMNS where TABLE_SCHEMA = (select database()) and TABLE_NAME=#{tableName}")
	public List<Map<String, ?>> selectListTableColumn(@Param("tableName") String tableName);

	/**
	 * 更新列名和类型
	 * @param tab
	 * tableName：表名
	 * oldColumn 原来的列名
	 * newColumn 新的列名
	 * newType 新的列类型
	 */
	@Update("alter table ${tableName} change column ${oldColumn}  ${newColumn}  ${newColumnType}")
	public void updateTable(Map<String, Object> tabColumnUpdate);
}
